//
//  ViewController.m
//  VideoChat
//
//  Created by user on 2019/3/20.
//  Copyright © 2019 烧烤有点辣. All rights reserved.
//

#import "ViewController.h"

#import "VideoViewController.h"

#import "DNKUdpRequestManager.h"
#import "NSString+IpAddress.h"
#import "FriendTableViewCell.h"
#import "UIAlertController+Blocks.h"
#import "VideoChatViewController.h"
#import "VideoTCPChatViewController.h"


#define KPort 38889
#define KConnect @"connect"
#define KBackMessage @"backMessage"
#define KBackMessageError @"backMessageError"
#define KPush @"pushTCPVideo"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *saveIPAdressArray;
@property (nonatomic, copy) NSString *locationIp;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [NSString getIPAddress];
    _saveIPAdressArray = [[NSMutableArray alloc] init];
    _locationIp = [NSString getIPAddress];
    [_saveIPAdressArray addObject:_locationIp];
    
    [[DNKUdpRequestManager shareUdpManager] initWithPort:KPort]; 
    
    __weak typeof(self) weakself = self;
    [[DNKUdpRequestManager shareUdpManager] CallMessageSuccessBlock:^(NSData *data,NSData *address, NSInteger tag) {

        NSString *ip = [[DNKUdpRequestManager shareUdpManager] hostFromAddress:address];
        NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 

        if ([KConnect isEqualToString:msg]) {

            NSString *message = [NSString stringWithFormat:@"来自%@的视频请求",ip];
            [UIAlertController showAlertInViewController:self withTitle:@"视频连接" message:message cancelButtonTitle:@"取消" destructiveButtonTitle:@"接收" otherButtonTitles:nil tapBlock:^(UIAlertController * _Nonnull controller, UIAlertAction * _Nonnull action, NSInteger buttonIndex) {
                if (buttonIndex == controller.cancelButtonIndex) {
                    [[DNKUdpRequestManager shareUdpManager] sendUdpWithData:[KBackMessageError dataUsingEncoding:NSUTF8StringEncoding] toIp:ip port:KPort Timeout:-1 tag:100];
                } else {
                    [[DNKUdpRequestManager shareUdpManager] sendUdpWithData:[KBackMessage dataUsingEncoding:NSUTF8StringEncoding] toIp:ip port:KPort Timeout:-1 tag:100];
                    [self performSegueWithIdentifier:KPush sender:ip];
                    return ;
                }
            }];

        } else if ([KBackMessage isEqualToString:msg]) {

            [self performSegueWithIdentifier:KPush sender:ip];

        } else if ([KBackMessageError isEqualToString:msg]) {

            [UIAlertController showAlertInViewController:self withTitle:@"视频连接" message:@"对方拒绝与你视频聊天" cancelButtonTitle:nil destructiveButtonTitle:@"确定" otherButtonTitles:nil tapBlock:nil];
        }

        if (![ip isEqualToString:_locationIp] &&
            ![ip isEqualToString:[NSString stringWithFormat:@"::ffff:%@",weakself.locationIp]]) {
            if ([[ip substringToIndex:2] isEqualToString:@"::"]) {
                return;
            }
            if (![weakself.saveIPAdressArray containsObject:ip]) {
                [weakself.saveIPAdressArray addObject:ip];

                [[DNKUdpRequestManager shareUdpManager] sendUdpWithData:[weakself.locationIp dataUsingEncoding:NSUTF8StringEncoding] toIp:ip port:KPort Timeout:-1 tag:100];
                
                [weakself.tableView reloadData];

            }
        }

    } failureBlock:^(NSString *message) {

    }];
    
//    [self performSegueWithIdentifier:KPush sender:@"192.168.1.100"];
}

//- (void)viewDidDisappear:(BOOL)animated {
//    [super viewDidDisappear:animated];
//    
//    [[DNKUdpRequestManager shareUdpManager] coloseUDP];
//}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[DNKUdpRequestManager shareUdpManager] coloseUDP];
}

- (IBAction)shuaxinAction:(UIBarButtonItem *)sender {
    VideoViewController *vc = [[VideoViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)scanAction:(UIBarButtonItem *)sender {
    
    [[DNKUdpRequestManager shareUdpManager] sendUdpBroadData:[@"hello" dataUsingEncoding:NSUTF8StringEncoding] port:KPort Timeout:-1 tag:100];
}

 

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _saveIPAdressArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FriendTableViewCell" forIndexPath:indexPath];
    cell.IpLabel.text = _saveIPAdressArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [[DNKUdpRequestManager shareUdpManager] sendUdpWithData:[KConnect dataUsingEncoding:NSUTF8StringEncoding] toIp:_saveIPAdressArray[indexPath.row] port:KPort Timeout:-1 tag:100]; 
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:KPush]) {
        VideoTCPChatViewController *vc = segue.destinationViewController;
        vc.addressIP = sender;
    }
    
}


@end

